window.onload=function(){
    //获取用户登录信息信息
    axios.defaults.withCredentials = true;

    let geohash='';
    //获取经纬度
    $().ajax({
        url:'https://elm.cangdu.org/v1/cities/240',
        method:'get',
        async:true,
        success:(res)=>{
            console.log(res);
            geohash=`${res.latitude},${res.longitude}`
            console.log(geohash);
               //获取地址
    $('#kh_schoolor').click(function(){
        window.location.href=`/pages/address/searchcity.html?id=${res.id}`
    })
        }

    })
 
    //获取用户信息
    axios.get('https://elm.cangdu.org/v1/user').then(res => {
        console.log(res);
        $('#kh_submit').click(function(){
            console.log($('#kh_schoolor').value());
            console.log(res.data.user_id);
            $().ajax({
                url:`https://elm.cangdu.org/v1/users/${res.data.user_id}/addresses  `,
                method:'post',
                async:true,
                data:{
                    user_id:parseInt(res.data.user_id),
                    address:$('#kh_schoolor').value(),
                    address_detail:$('#kh_address').value(),
                    geohash:geohash,
                    name:$('#kh_name').value(),
                    phone:$('#kh_phone').value(),
                    tag:'666',
                    sex:1,
                    phone_bk:$('#kh_phoneor').value(),
                    tag_type:2,
                },
                success:(data)=>{ 
                    console.log(data);
                    if(data.status==0){
                        Toast('warning',data.message)
                    }else if(data.status==1){
                        Toast('success',data.success)
                    }
                }
            })
        })
    
    })
    let url = window.location.href;
    let paramStr = url.split('?')[1];
    let paramArr = paramStr.split('&');
    let paramObj = {};
    for (let i = 0; i < paramArr.length; i++) {
        let p_item = paramArr[i];
        paramObj[p_item.split('=')[0]] = p_item.split('=')[1]
    }
    console.log(paramObj);
    //根据经纬度获取地址
    let kh_schoolor=document.getElementById('kh_schoolor')

    $().ajax({
        url:`https://elm.cangdu.org/v2/pois/${paramObj.geohash}`,
        method:'get',
        success:res=>{
            
            kh_schoolor.value=res.name
        }
    }) 

}